package com.zzyl.nursing.job;

import java.time.LocalDateTime;
import java.util.Date;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.DeviceData;
import com.zzyl.nursing.domain.DeviceDataList;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.mapper.DeviceDataListMapper;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.vo.DeviceDataListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.*;


@Component("averageByDay")
public class AverageByDay {
    @Autowired
    private DeviceDataMapper  deviceDataMapper;

    @Autowired
    private DeviceDataListMapper  deviceDataListMapper;

    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;

    public void averageDataByDay() {
        //计算每小时的iot数据平均值
        //获取所有设备的数据
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime end = now.withMinute(0).withSecond(0).withNano(0);
        LocalDateTime start = end.minusHours(1);
            List<DeviceDataList> deviceDataList =deviceDataMapper.selectByHour(start,end);
            if (CollUtil.isNotEmpty(deviceDataList)) {
                deviceDataList.forEach(DataList -> {
                    DeviceDataList  deviceDataListVo = new DeviceDataList();
                    deviceDataListVo.setDateTime(end);
                    deviceDataListVo.setIotId(DataList.getIotId());
                    deviceDataListVo.setFunctionId(DataList.getFunctionId());
                    deviceDataListVo.setDay(end.toLocalDate());
                    //平均值保留小数点后两位
                    deviceDataListVo.setDataValue(Math.round(DataList.getDataValue() * 100) / 100.0);
                    deviceDataListMapper.insertDeviceDataList(deviceDataListVo);
                });
            }
    }

}
